package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.process.ProcessAPI;
import com.example.tool.erp.sdk.api.process.dto.*;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Process
{
	public static void main(String[] args) throws WdtErpException
	{
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
		ProcessAPI processAPI = ApiFactory.get(client, ProcessAPI.class);

//		uploadBomTest(processAPI);
//		searchProcess(processAPI);
//		uploadProcess(processAPI);
		searchBomTest(processAPI);
	}

	private static void uploadBomTest(ProcessAPI api) throws WdtErpException
	{
		BomUploadRequest request = new BomUploadRequest();
		request.setBomName("sdkUploadTest");
		request.setFee(new BigDecimal("3"));
		request.setRemark("sdkUploadTest");

		BomUploadRequest.Detail productDetail = new BomUploadRequest.Detail();
		productDetail.setIsProduct(true);
		productDetail.setNum(new BigDecimal("5"));
		productDetail.setSpecNo("gy01");
		productDetail.setRemark("product");

		BomUploadRequest.Detail materialDetail = new BomUploadRequest.Detail();
		materialDetail.setSpecNo("gy02");
		materialDetail.setNum(new BigDecimal("2"));
		materialDetail.setIsProduct(false);
		materialDetail.setRemark("material");

		List<BomUploadRequest.Detail> detailList = new ArrayList<>();
		detailList.add(productDetail);
		detailList.add(materialDetail);

		request.setDetailList(detailList);

		BomUploadResponse response = api.uploadBom(request);

		if (response == null || response.getStatus() > 0)
		{
			System.out.println("error");
		}
		else
		{
			System.out.println("success");
		}

	}

	private static void searchProcess(ProcessAPI api) throws WdtErpException
	{
		int pageSize = 10;
		Pager pager = new Pager(pageSize, 0, true);

		ProcessSearchRequest request = new ProcessSearchRequest();
		request.setStartTime("2022-09-01");
		request.setEndTime("2022-09-30");

		ProcessSearchResponse response = api.searchProcess(request, pager);
		Integer total = response.getTotal();
		if (total == null || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false);
		for (int i = totalPage; i >= 0; --i)
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:"+pageSize+" page_no:"+i+" ");
			response = api.searchProcess(request, pager);
			System.out.println("处理数据");
		}
	}

	private static void uploadProcess(ProcessAPI api) throws WdtErpException
	{
		ProcessUploadRequest request = new ProcessUploadRequest();
		request.setOutWarehouseNo("ytz");
		request.setInWarehouseNo("ytz1");
		request.setOperatorName("聪聪");
		request.setProducerNo("007");
		request.setProcessCount(1);
		request.setFlagName("");
		request.setBomName("");
		request.setTotalFee(new BigDecimal("10"));
		request.setRemark("remark");
		request.setCheck(false);

		List<ProcessUploadRequest.Detail> detailList = new ArrayList<>();
		request.setDetailList(detailList);

		ProcessUploadRequest.Detail productDetail = new ProcessUploadRequest.Detail();
		ProcessUploadRequest.Detail materialDetail = new ProcessUploadRequest.Detail();

		productDetail.setSpecNo("gy01");
		productDetail.setNum(new BigDecimal("5"));
		productDetail.setProduct(true);

		materialDetail.setSpecNo("gy02");
		materialDetail.setNum(new BigDecimal("3"));
		materialDetail.setProduct(false);

		detailList.add(productDetail);
		detailList.add(materialDetail);

		ProcessUploadResponse response = api.uploadProcess(request);
		if (response == null || response.getStatus() > 0)
		{
			System.out.println("error");
		}
		else
		{
			System.out.println("processNo: "+response.getProcessNo());
			System.out.println("processId: "+response.getProcessId());
		}
	}

	private static void searchBomTest(ProcessAPI api) throws WdtErpException
	{
		int pageSize = 10;
		Pager pager = new Pager(pageSize, 0, true);

		BomSearchRequest request = new BomSearchRequest();
		request.setStartTime("2022-09-01");
		request.setEndTime("2022-09-30");

		BomSearchResponse response = api.searchBom(request, pager);
		Integer total = response.getTotal();
		if (total == null || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false);
		for (int i = totalPage; i >= 0; --i)
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:"+pageSize+" page_no:"+i+" ");
			response = api.searchBom(request, pager);
			System.out.println("处理数据");
		}
	}
}
